package iammry.com.activity;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.lidroid.xutils.view.annotation.ViewInject;
import java.util.Calendar;
import iammry.com.R;
import iammry.com.base.BaseActivity;
import iammry.com.mode.SelectLayout;
import iammry.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import iammry.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer;

/**
 * 主界面
 */
public class MainActivity extends BaseActivity implements OnClickListener {

    @ViewInject(R.id.iv_chartview)
    ImageView ivChartview;
    @ViewInject(R.id.ll_sensitive)
    LinearLayout llSensitive;
    @ViewInject(R.id.ll_protect)
    LinearLayout llProtect;
    @ViewInject(R.id.ll_depth)
    LinearLayout llDepth;
    @ViewInject(R.id.ll_strong)
    LinearLayout llStrong;
    @ViewInject(R.id.ll_polishing)
    LinearLayout llPolishing;
    private SlidingMenu menu;

    @ViewInject(R.id.ll_root_huadong)
    LinearLayout ll_root_huadong;

    @ViewInject(R.id.ll_main_root)
    LinearLayout ll_root;

    @ViewInject(R.id.verticalProgressBar1)
    ProgressBar verticalProgressBar1;

    @ViewInject(R.id.verticalProgressBar2)
    ProgressBar verticalProgressBar2;

    @ViewInject(R.id.tv_battery_text)
    TextView tv_battery_text;

    @ViewInject(R.id.tv_toothbrush_text)
    TextView tv_toothbrush_text;

    @ViewInject(R.id.ll_night)
    LinearLayout ll_night;


    @ViewInject(R.id.ll_morning)
    LinearLayout ll_morning;

    @ViewInject(R.id.fl_bg)
    FrameLayout fl_bg;


    CanvasTransformer mTransformer;
    private FragmentManager manager;
    private static final int MSG_PROGRESS_UPDATE = 0x110;
    private static final int LOADDATA = 0x111;
    private int mScreenWidth;
    ImageView ivBrushAnimal;


    private GestureDetector detector;

    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_PROGRESS_UPDATE:

                    int progress2 = verticalProgressBar2.getProgress();
                    verticalProgressBar2.setProgress(++progress2);

                    tv_battery_text.setText(progress2 - 1 + "%");
                    int progress1 = verticalProgressBar1.getProgress();
                    verticalProgressBar1.setProgress(++progress1);
                    tv_toothbrush_text.setText(progress1 - 1 + "%");
                    if (progress1 == 100 || progress2 == 100) {
                        mHandler.removeMessages(MSG_PROGRESS_UPDATE);

                    }
                    mHandler.sendEmptyMessageDelayed(MSG_PROGRESS_UPDATE, 100);
                    break;

                case LOADDATA:


                    break;

            }

        }

    };

    @SuppressLint({"NewApi", "ClickableViewAccessibility"})
    @Override
    protected void initView() {
        setLayout(this, R.layout.mainactivity_layout);
        // FontManger.changeFonts(ll_root,getBaseContext());
        mScreenWidth = MainActivity.this.getResources().getDisplayMetrics().widthPixels;
        menu = new SlidingMenu(this);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置手势模式
        menu.setBehindOffset(mScreenWidth / 2 - 20);// 打开菜单后余下的主界面宽度
        menu.setFadeDegree(0.35f);// 淡入淡出的比例
        menu.setFadeEnabled(true);// 设置是否有渐变
        menu.setMode(SlidingMenu.LEFT);
        //menu.setBehindScrollScale(0);
        menu.setBehindScrollScale(0.133f);// 设置渐变率
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.fragment_left_menu);// 设置滑动菜单的视图界面
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        manager = getSupportFragmentManager();
        // menu.setAboveOffset(20);
        ivBrushAnimal = (ImageView) menu.findViewById(R.id.iv_brushAnimal);
        ivBrushAnimal.setBackgroundResource(R.drawable.synchrodata_animal);
        mHandler.sendEmptyMessage(MSG_PROGRESS_UPDATE);
        //设置手势
        setGestureListener();

        menu.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
            @Override
            public void onOpened() {
                startAnima();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        menu.toggle();
                    }
                }, 1000);

            }
        });
        menu.setOnClosedListener(new SlidingMenu.OnClosedListener() {
            @Override
            public void onClosed() {

            }
        });


    }

    @Override
    protected void initData() {
        initTime();

        selectBrushMode(1);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ivChartview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(ChartActivity.class);

            }
        });

        ll_morning.setOnClickListener(this);
        ll_night.setOnClickListener(this);
        llSensitive.setOnClickListener(this);
        llProtect.setOnClickListener(this);
        llDepth.setOnClickListener(this);
        llStrong.setOnClickListener(this);
        llPolishing.setOnClickListener(this);


    }

    private View view;
    private LinearLayout set;

    private void startAnima() {

        AnimationDrawable anim = (AnimationDrawable) ivBrushAnimal
                .getBackground();
        anim.start();

    }

    public void initTime() {
        long time = System.currentTimeMillis();
        final Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int hour = mCalendar.get(Calendar.HOUR);
        int apm = mCalendar.get(Calendar.AM_PM);

        //apm 为0 为上午 1为下午
        if (apm == 0) {
            swtichNightAndNight(0);
        } else {
            swtichNightAndNight(1);

        }

    }


    public void swtichNightAndNight(int type) {

        if (type == 0) {
            ll_morning.setBackgroundResource(R.mipmap.home_btn_morning_h);
            ll_night.setBackgroundResource(R.mipmap.home_btn_evening_n);

        } else {
            ll_morning.setBackgroundResource(R.mipmap.home_btn_morning_n);
            ll_night.setBackgroundResource(R.mipmap.home_btn_evening_h);

        }

    }

    public void selectBrushMode(int type) {
        switch (type) {
            case 1:
                fl_bg.setBackgroundResource(R.mipmap.home_view_percentage1);
                llSensitive.setBackgroundResource(R.mipmap.home_btn_sensitive_h);
                llProtect.setBackgroundResource(R.mipmap.home_btn_protect_n);
                llDepth.setBackgroundResource(R.mipmap.home_btn_depth_n);
                llStrong.setBackgroundResource(R.mipmap.home_btn_strong_n);
                llPolishing.setBackgroundResource(R.mipmap.home_btn_polishing_n);

                break;
            case 2:
                fl_bg.setBackgroundResource(R.mipmap.home_view_percentage2);
                llSensitive.setBackgroundResource(R.mipmap.home_btn_sensitive_n);
                llProtect.setBackgroundResource(R.mipmap.home_btn_protect_h);
                llDepth.setBackgroundResource(R.mipmap.home_btn_depth_n);
                llStrong.setBackgroundResource(R.mipmap.home_btn_strong_n);
                llPolishing.setBackgroundResource(R.mipmap.home_btn_polishing_n);
                break;
            case 3:
                fl_bg.setBackgroundResource(R.mipmap.home_view_percentage3);
                llSensitive.setBackgroundResource(R.mipmap.home_btn_sensitive_n);
                llProtect.setBackgroundResource(R.mipmap.home_btn_protect_n);
                llDepth.setBackgroundResource(R.mipmap.home_btn_depth_h);
                llStrong.setBackgroundResource(R.mipmap.home_btn_strong_n);
                llPolishing.setBackgroundResource(R.mipmap.home_btn_polishing_n);

                break;
            case 4:
                fl_bg.setBackgroundResource(R.mipmap.home_view_percentage4);
                llSensitive.setBackgroundResource(R.mipmap.home_btn_sensitive_n);
                llProtect.setBackgroundResource(R.mipmap.home_btn_protect_n);
                llDepth.setBackgroundResource(R.mipmap.home_btn_depth_n);
                llStrong.setBackgroundResource(R.mipmap.home_btn_strong_h);
                llPolishing.setBackgroundResource(R.mipmap.home_btn_polishing_n);
                break;
            case 5:
                fl_bg.setBackgroundResource(R.mipmap.home_view_percentage5);
                llSensitive.setBackgroundResource(R.mipmap.home_btn_sensitive_n);
                llProtect.setBackgroundResource(R.mipmap.home_btn_protect_n);
                llDepth.setBackgroundResource(R.mipmap.home_btn_depth_n);
                llStrong.setBackgroundResource(R.mipmap.home_btn_strong_n);
                llPolishing.setBackgroundResource(R.mipmap.home_btn_polishing_h);

                break;


        }


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_morning:
                swtichNightAndNight(0);
                break;
            case R.id.ll_night:
                swtichNightAndNight(1);
                break;
            case R.id.ll_sensitive:
                selectBrushMode(1);
                break;
            case R.id.ll_protect:
                selectBrushMode(2);
                break;
            case R.id.ll_depth:
                selectBrushMode(3);
                break;
            case R.id.ll_strong:
                selectBrushMode(4);
                break;
            case R.id.ll_polishing:
                selectBrushMode(5);
                break;


        }

    }


    //手势
    private void setGestureListener() {

        ll_root_huadong.setOnTouchListener(new OnTouchListener() {
            private float mPosX;
            private float mPosY;
            private float mCurPosX;
            private float mCurPosY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        mPosX = event.getX();

                        //System.out.print("mPosX"+mPosX);
                        mPosY = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mCurPosX = event.getX();
                        mCurPosY = event.getY();

                        break;
                    case MotionEvent.ACTION_UP:

                        if (mCurPosY - mPosY > 0
                                && (Math.abs(mCurPosY - mPosY) > 25)) {
                            // 向下滑動

                        } else if (mCurPosY - mPosY < 0
                                && (Math.abs(mCurPosY - mPosY) > 25)) {

                            view = View.inflate(context, R.layout.main_upglide_layout, null);

                            new SelectLayout(context, view);

                            LinearLayout set = (LinearLayout) view.findViewById(R.id.ll_set);
                            //用户协议
                            LinearLayout feedback = (LinearLayout) view.findViewById(R.id.ll_feedback);
                            LinearLayout ll_instruction = (LinearLayout) view.findViewById(R.id.ll_instruction);
                            LinearLayout ll_findnewversion = (LinearLayout) view.findViewById(R.id.ll_findnewversion);
                            LinearLayout ll_useragreement = (LinearLayout) view.findViewById(R.id.ll_useragreement);

                            //设置
                            setLayoutListener(set, SetActivity.class);
                            //反馈
                            //   setLayoutListener(feedback, FeedbackActivity.class);

                            //使用说明
                            setLayoutListener(ll_instruction, InstructionsActivity.class);
                            setLayoutListener(ll_useragreement, UseragreementActivity.class);

                            feedback.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    FeedbackAPI.openFeedbackActivity();
                                }
                            });
                        }

                        break;
                }

                return true;
            }

        });
    }

    private void setLayoutListener(LinearLayout ll, final Class clazz) {
        //设置


        ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                startActivity(clazz);

            }
        });


    }
}
